#include "head_2.h"
#include "sem.h"

sendEnv envMsg;

linknode *H_trf = NULL;
linknode *E_trf = NULL;

linknode *H_sql = NULL;
linknode *E_sql = NULL;
char dev_camera_mask;
int dev_camera_fd;

pthread_cond_t cond_camera;
pthread_cond_t cond_analysis;
pthread_cond_t cond_sqlite;
pthread_cond_t cond_uart_cmd;
pthread_cond_t cond_buzzer;

pthread_mutex_t mutex_camera;
pthread_mutex_t mutex_analysis;
pthread_mutex_t mutex_transfer;
pthread_mutex_t mutex_linklist;
pthread_mutex_t mutex_slinklist;
pthread_mutex_t mutex_global;
pthread_mutex_t mutex_buzzer;
pthread_mutex_t mutex_sqlite;

extern void * pthread_analysis ();			
extern void * pthread_uart_transfer();	
extern void * pthread_client_request() ;
extern void get_queue_id();
extern void * list_create();
extern void * pthread_uart_cmd();
extern void * pthread_sqlite();
extern void * pthread_buzzer();
extern void * pthread_camera();


int main(int argc, char *argv[])
{
	pthread_t id_client_request;
	pthread_t id_transfer;
	pthread_t id_analysis;
	pthread_t id_uart_cmd;
	pthread_t id_buzzer;
	pthread_t id_sqlite;
	pthread_t id_camera;
	struct sockaddr_in myaddr;
	
	pthread_mutex_init (&mutex_analysis, NULL);  
	pthread_mutex_init (&mutex_global, NULL);  
	pthread_mutex_init (&mutex_linklist, NULL);
	pthread_mutex_init (&mutex_transfer, NULL);
	pthread_mutex_init (&mutex_slinklist, NULL);
	pthread_mutex_init (&mutex_buzzer, NULL);
	pthread_mutex_init (&mutex_sqlite, NULL);
	pthread_mutex_init (&mutex_camera,NULL);
	
	pthread_cond_init (&cond_camera,NULL);
	pthread_cond_init (&cond_analysis, NULL); 
	pthread_cond_init (&cond_sqlite, NULL); 
	pthread_cond_init (&cond_uart_cmd, NULL); 
	pthread_cond_init (&cond_buzzer, NULL); 
	
	get_queue_id();
	list_create();

	if(pthread_create(&id_client_request, NULL, pthread_client_request,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}

	if(pthread_create(&id_analysis, NULL, pthread_analysis,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}

	if(pthread_create(&id_uart_cmd, NULL, pthread_uart_cmd,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
	
	if(pthread_create(&id_transfer, NULL, pthread_uart_transfer,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
	
	if(pthread_create(&id_buzzer, NULL, pthread_buzzer,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
	
	if(pthread_create(&id_sqlite, NULL, pthread_sqlite,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
	if(pthread_create(&id_camera, NULL, pthread_camera,NULL) < 0)
	{
		perror("pthread_create");
		exit(1);
	}
	
	pthread_join (id_buzzer, NULL);
	pthread_join (id_sqlite, NULL);
	pthread_join (id_client_request, NULL);
	pthread_join (id_analysis, NULL);
	pthread_join (id_uart_cmd, NULL);
	pthread_join (id_transfer, NULL);
	pthread_join (id_sqlite, NULL);
	pthread_join (id_camera,NULL);
}
